package controller.hansot;

import hansot.to.MemberTO;
import hansot.to.MenuTO;
import hansot.to.ZipcodeTO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.hansot.CustomerService;

@Controller
public class ManagerController {
	private CustomerService service;

	public ManagerController(CustomerService service) {
		this.service = service;
	}

	@RequestMapping("/menuRegister.do")
	public ModelAndView menuJoin(MenuTO menuto) {
		ModelAndView mv = new ModelAndView();
		try {
			service.joinMenu(menuto);
			mv.setViewName("등록성공/menuRegister_success");
			mv.addObject("menuto", menuto);
		} catch (Exception e) {
			mv.setViewName("/error");
			mv.addObject("error_message", e.getMessage());
			e.printStackTrace();
		}
		return mv;
	}

	// 메뉴 정보 조회 처리
	@RequestMapping("/menuSearch.do")
	public ModelAndView searchMenuByName(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = null;
		String name = request.getParameter("name");
		try {
			ArrayList<MenuTO> list = service.searchMenuByName(name);
			if(list!=null){
				HashMap map = new HashMap();
				map.put("menu_list", list);
				mv = new ModelAndView("/menuSearch_form", map);
			}else{
				mv = new ModelAndView("/menuSearch_form","message", "없는 제품입니다. 확인 후 검색하세요.");
			}
		} catch (SQLException e) {
			mv = new ModelAndView("/error", "error_message",
					"이름으로 검색도중 오류발생 : " + e.getMessage());
		}
		return mv;
	}

	// 메뉴 정보 수정 처리
	@RequestMapping("/menuModify.do")
	public ModelAndView modifyMenu(HttpServletRequest request,
			HttpServletResponse response, MenuTO menuto) {
		ModelAndView mv = null;
		try {
			// 정보 수정 비지니스 로직에 요청
			service.modifyMenu(menuto);
			MenuTO meto = menuto;
			mv = new ModelAndView("/menuModify_success", "meto", meto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("/error", "error_message", e.getMessage());
		}
		return mv;
	}

	// 메뉴 삭제 처리
	@RequestMapping("/menuDelete.do")
	public ModelAndView removeMenu(HttpServletRequest request,
			HttpServletResponse response, String menuId) {
		ModelAndView mv = null;
		try {
			service.removeMenuById(menuId);
			mv = new ModelAndView("/menuDelete_success", "message", menuId
					+ "삭제되었습니다.");
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("/error.jsp", "error_message", e.getMessage());
		}
		return mv;
	}

	// 회원 조회 처리
	@RequestMapping("/memberSearch.do")
	public ModelAndView searchMemberById(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = null;
		String id = request.getParameter("memberId");
		try {
			MemberTO mto = service.searchMemberById(id);
			if (mto != null) {
				request.setAttribute("mto", mto);
				ZipcodeTO zto = service.getZipcodeBySeq(mto.getSeq());
				if (zto.getBunji() != null) {
					request.setAttribute("smAddress", zto.getSido() + " " + zto.getGugun() + " "	+ zto.getDong() + " " + zto.getBunji());
				} else {
					request.setAttribute("smAddress", zto.getSido() + " " + zto.getGugun() + " "+ zto.getDong());
				}
				request.setAttribute("zipcode", zto.getZipcode());
				mv = new ModelAndView("/memberSearch_form");
			} else {
				mv = new ModelAndView("/memberSearch_form","message", "없는 회원입니다. 확인 후 검색하세요.");
			}
		} catch (SQLException e) {
			mv = new ModelAndView("/error", "error_message",
					"이름으로 검색도중 오류발생 : " + e.getMessage());
		}
		return mv;
	}
}
